﻿using AbpApplication.InPut;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using Microsoft.AspNetCore.SignalR.Client;
using NewWms.Apiservice;
using NewWms.Apiservice.Dto;
using NewWms.Apiservice.InPut;
using NewWms.Extend;
using Prism.Mvvm;
using Prism.Regions;
using Reconstruct_Desktop.Apiservice.Dto;
using SingalR.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using wms_wpf.Apiservice;
using wms_wpf.InPut;
using MessageBox = HandyControl.Controls.MessageBox;

namespace NewWms.ViewModels
{
    public partial class BoxShelfViewModel : BindableBase, INavigationAware
    {
        //define
        private IViewService viewService;
        private IBaseService baseService;
        private IEssService essService;
        public BoxShelfViewModel()
        { 
            viewService = new ViewService();
            baseService = new BaseService();
            essService = new EssService();
            InPut_ViewService6=new InPut_ViewService6();
            InPut_ViewService4 = new InPut_ViewService4();
            InPut_ViewService6.PageSize = 50;
            InPut_ViewService6.PageIndex = 1;
            View_Box_WarehouseLineDtos = new List<View_Box_WarehouseLineDto>();
            View_MaterialDtos = new List<View_MaterialDto>();
            View_TaskOrderDtos = new List<View_TaskOrderDto>();
          
           
        }
        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        private InPut_ViewService6 inPut_ViewService6;
        public InPut_ViewService6 InPut_ViewService6
        {
            get { return inPut_ViewService6; }
            set { inPut_ViewService6 = value;RaisePropertyChanged(); }
        }
        private List<View_TaskOrderDto> view_TaskOrderDtos;
        public List<View_TaskOrderDto> View_TaskOrderDtos
        {
            get { return view_TaskOrderDtos; }
            set { view_TaskOrderDtos = value; RaisePropertyChanged(); }
        }
        private List<View_Box_WarehouseLineDto> view_Box_WarehouseLineDtos;
        public List<View_Box_WarehouseLineDto> View_Box_WarehouseLineDtos
        {
            get { return view_Box_WarehouseLineDtos; }
            set { view_Box_WarehouseLineDtos = value;  RaisePropertyChanged(); }
        }
        private List<View_MaterialDto> view_MaterialDtos;
        public List<View_MaterialDto> View_MaterialDtos
        {
            get { return view_MaterialDtos; }
            set { view_MaterialDtos = value; RaisePropertyChanged(); }
        }
        private InPut_ViewService4 inPut_ViewService4;
        public InPut_ViewService4 InPut_ViewService4
        {
            get { return inPut_ViewService4; }
            set { inPut_ViewService4 = value; RaisePropertyChanged(); }
        }
        private List<InlethouseLineDto> inlethouseModels;





        //功能
        [RelayCommand]
        public void Method(string obj)
        {
            switch (obj)
            {
                case "View_Box_Warehoueline":
                    View_Box_Warehoueline();break;
                         case "View_Box_Warehoueline2":
                    View_Box_Warehoueline2(); break;
            }
        }
        //箱盒视图
        [RelayCommand]
        private async void View_MaterialDto(string BoxCode)
        {
            InPut_ViewService4.BoxPositionCode = BoxCode;
            InPut_ViewService4.PageIndex = 1;
            InPut_ViewService4.PageSize = 200;
            Tuple<List<View_MaterialDto>, long?, long?> tuple = await viewService.View_Material(InPut_ViewService4);
            if (tuple.Item1 != null)
            {
                View_MaterialDtos = tuple.Item1;
            }
        }
        //箱架视图
        private async void View_Box_Warehoueline()
        {
          
            Tuple<List<View_Box_WarehouseLineDto>, long?, long?> tuple = await viewService.View_Box_WarehouseLine(InPut_ViewService6);
            if (tuple.Item1 != null)
            {
                View_Box_WarehouseLineDtos = tuple.Item1;
            }
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
        }
        //空箱视图
        private async void View_Box_Warehoueline2()
        {
            InPut_ViewService6.IsEmpty = true;
            Tuple<List<View_Box_WarehouseLineDto>, long?, long?> tuple = await viewService.View_Box_WarehouseLine(InPut_ViewService6);
            if (tuple.Item1 != null)
            {
                View_Box_WarehouseLineDtos = tuple.Item1;
            }
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
        }
        //下架 agv任务
        [RelayCommand]
        private async void CreateTaskOrderOut(IList SelectItems)
        {
            InPut_BaseService13 inPut = new InPut_BaseService13();
            inPut.InlethouseCode = GlobalVariable.InletCode;
            if (string.IsNullOrEmpty(inPut.InlethouseCode))
            {
                MessageBox.Info("请输入出库口");
                return;
            }
            foreach (View_Box_WarehouseLineDto item in SelectItems)
            {
                inPut.BoxCodes. Add(item.BoxCode);
            }
            Tuple<bool, List<TaskOrderDto>> tuple = await baseService.CreateTaskOrderOut(inPut);
            if (tuple.Item1)
            {
                //foreach (var item in tuple.Item2)
                //{
                //    await essService.sendCommand("RED",item.InlethouseCode,item.BoxCode.Substring(item.BoxCode.Length - 4));
                //}
                ////亮灯   
                View_TaskOrder();
            }

        }
        //加入列表
        [RelayCommand]
        private async void AddTaskOrderOut(string BoxCode)
        {
            if (string.IsNullOrEmpty(BoxCode))
            {
                MessageBox.Info("请输入箱号");
                return;
            }
            InPut_BaseService13 inPut = new InPut_BaseService13();
            inPut.InlethouseCode = GlobalVariable.InletCode;
            inPut.BoxCodes.Add(BoxCode);
            if (string.IsNullOrEmpty(inPut.InlethouseCode))
            {
                MessageBox.Info("请输入出库口");
                return;
            }
            Tuple<bool, List<TaskOrderDto>> tuple = await baseService.CreateTaskOrderOut(inPut);
            if (tuple.Item1)
            {
                //foreach (var item in tuple.Item2)
                //{
                //    await essService.sendCommand("RED", item.InlethouseCode, item.BoxCode.Substring(item.BoxCode.Length - 4));
                //}
                ////亮灯   
                View_TaskOrder();
            }
        }
        //任务视图
        private async void View_TaskOrder()
        {
            InPut_ViewService5 inPut_ViewService5 = new InPut_ViewService5();
            inPut_ViewService5.PageIndex = 1;
            inPut_ViewService5.PageSize = 50;
            inPut_ViewService5.Type = "出库";
            inPut_ViewService5.State = "未下发";
            inPut_ViewService5.InlethouseCode= GlobalVariable.InletCode;
            Tuple<List<View_TaskOrderDto>, long?, long?> tuple = await viewService.View_TaskOrder(inPut_ViewService5);
            if (tuple != null)
            {
                View_TaskOrderDtos = tuple.Item1;
            }
        }
        //删除agv任务
        [RelayCommand]
        private async void DeleteTaskOrder(IList SelectItems)
        {

            List<string> InPut = new List<string>();
            foreach (View_TaskOrderDto item in SelectItems)
            {
                InPut.Add(item.TaskOrderCode);
            }
            bool res = await baseService.DeleteTaskOrder(InPut);
            if (res) View_TaskOrder();

        }
        //发送任务
        [RelayCommand]
        private async void TaskToEss(IList SelectItems)
        {
            foreach (View_TaskOrderDto item in SelectItems)
            {
                Ess_Input ess_Input = new Ess_Input();
                ess_Input.taskType = "carry";
                ess_Input.taskGroupCode = "taskGroupCode-001";
                ess_Input.groupPriority = 0;
                List<Tasks> data = new List<Tasks>();
                Tasks ta = new Tasks();
                ta.taskCode = item.TaskOrderCode;
                ta.taskPriority = 0;
                data.Add(ta);
                ess_Input.tasks = data;
                TaskDescribe td = new TaskDescribe();
                td.containerCode = item.BoxCode;
                td.containerType = "CT_KUBOT_STANDARD";
                td.storageTag = "";
                td.fromLocationCode = item.WarehouseCode;
                td.toLocationCode = item.InlethouseCode;
                td.toStationCode = "haiport_station_1";
                td.isPutaway = false;
                ta.taskDescribe = td;
                string res = await essService.CreateTask(ess_Input);
                if (res.Contains("OK"))
                {
                    InPut_BaseService11 inPut = new InPut_BaseService11();
                    inPut.TaskOrderCode = item.TaskOrderCode;
                    inPut.Type = "已下发";
                    await baseService.UpdateTaskOrderState(inPut);                 
                    await baseService.CreateLog("下架", $"发送了下架任务{item.BoxCode}-{item.WarehouseCode}-{item.InlethouseCode}");
                }
            }
            View_TaskOrder();
        }



        //主页设置
        private string title = "料箱下架";
        public string Title
        {
            get { return title; }
            set { title = value; RaisePropertyChanged(); }
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            View_Box_Warehoueline();
            View_TaskOrder();
        }
    }
}
